﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Model;
using System.Data.SqlClient;
using System.Collections.ObjectModel;

[assembly: CLSCompliant(true)]
namespace University.Workshop.DataAccess
{
    public static class AppointmentDataAccess
    {

        static MedicalCenterEntities entities = MedicalCenterEntities.Instance;

        /// <summary>
        /// This method Inserts a new appointment in the database
        /// </summary>
        /// <param name="newAppointment">Appointment to Insert</param>
        /// <returns>return true if the appointment has been inserted correctly</returns>
        public static bool InsertNewAppointment(Appointment newAppointment)
        {
            bool verify = false;
            try
            {
                if (newAppointment != null)
                {
                    entities.Appointments.Add(newAppointment);
                    entities.SaveChanges();
                    return true;
                }
            }
            catch (SqlException)
            {
                verify = false;
            }
            return verify;
        }


        public static ICollection<Appointment> SearchAppointment(string id)
        {
            var appointments = from appointment in entities.Appointments
                               where appointment.PatientId == id
                               select appointment;

            return appointments.ToList();
        }

        public static ICollection<Appointment> GetAppointmentsByDate(string date)
        {
            IQueryable<Appointment> appointments = from appointment in entities.Appointments
                                                   where appointment.Date == date
                                                   select appointment;

            return appointments.ToList();
        }

        public static ICollection<MedicalSupplies> GetMedicalAppointmentByPatientId(string id)
        {
            var medicalSupplies = from m in entities.MedicalSupplies
                                  where m.ItemName.StartsWith(id)
                                  select m;
            return medicalSupplies.ToList();
        }


        public static void ModifyAppointmentDate(Appointment item)
        {
            try
            {
                Appointment apointment = entities.Appointments.First(m => m.AppointmentId == item.AppointmentId);
                apointment.Date = item.Date;
                apointment.Description = "Posponed";
                entities.SaveChanges();
            }
            catch(NullReferenceException)
            {
            }

        }

 

        public static ICollection<Appointment> GetAppointmentsToday(string doctorId, string dateToday)
        {
            var appointments = from app in entities.Appointments
                               where app.DoctorId == doctorId
                               where app.Date == dateToday
                               select app;
            return appointments.ToList();
        }


        public static ICollection<Appointment> GetPostponedAppointments(string doctorId)
        {
            var appointments = from app in entities.Appointments
                               where app.DoctorId == doctorId
                               where app.Description == "Postponed"
                               select app;
            return appointments.ToList();
        }

        public static ICollection<Appointment> AppointmentReminder(string dateTomorrow)
        {
            var appointments = from app in entities.Appointments
                               where app.Date == dateTomorrow
                               select app;
            return appointments.ToList();
        }
        public static void CancelAppointmentDate(Appointment item)
        {
            try
            {
                Appointment apointment = entities.Appointments.First(m => m.AppointmentId == item.AppointmentId);
                apointment.Date = item.Date;
                apointment.Description = "Cancelled";
                entities.SaveChanges();
            }
            catch (NullReferenceException)
            {
            }

        }
    }
}



